﻿#include "MainWindow.h"

#include "MyDialog.h"

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    // Avoid the window cannot maximize issue on Windows 10
    setMinimumSize(QSize(1024, 768));
    this->setWindowTitle("Qt GUI Builder");
    this->setWindowIcon(QIcon(":/icons/app.svg"));
    this->setAutoFillBackground(true);
    builder.setMainWindow(this);

    setupActions();
    setupMenuBar();
    setupToolBar();
    setupStatusBar();
    setupCentralWidget();
    setupDockWidgets();
}

MainWindow::~MainWindow() {}

void MainWindow::setupActions() {}

void MainWindow::setupMenuBar()
{
    builder.setMenuBar(this->menuBar())
        .addMenu("&File")
        .addMenuAction(new QAction("New", this))
        .addMenuAction(new QAction("Open", this))
        .addMenuAction(new QAction("Save", this))
        .addMenuAction(new QAction("Save As", this))
        .addMenuSeparator()
        .addMenuAction(new QAction("Exit", this))
        .addMenu("&Edit")
        .addMenuAction(new QAction("Cut", this))
        .addMenuAction(new QAction("Copy", this))
        .addMenuAction(new QAction("Paste", this))
        .addMenu("&Help")
        .addMenuAction(new QAction("About", this));
}

void MainWindow::setupToolBar()
{
    QAction* action = new QAction("Options", this);
    connect(action, &QAction::triggered, this, &MainWindow::onShowOptionsDialog);
    builder.addToolBar(this)
        .addToolBarAction(new QAction("New", this))
        .addToolBarAction(new QAction("Open", this))
        .addToolBarAction(new QAction("Save", this))
        .addToolBarAction(new QAction("Save As", this))
        .addToolBarSeparator()
        .addToolBarAction(new QAction("Exit", this))
        .addToolBar(this)
        .addToolBarAction(new QAction("Cut", this))
        .addToolBarAction(new QAction("Copy", this))
        .addToolBarAction(new QAction("Paste", this))
        .addToolBar(this)
        .addToolBarAction(action);
}
void MainWindow::setupStatusBar()
{
    builder.setStatusBar(this->statusBar())
        .addStatusBarIcon("connected", QIcon(":/icons/lightGreen+a.svg"), "connected")
        .addStatusBarLabel("copyright", "copyright © 2024");
}

void MainWindow::setupCentralWidget()
{
    // builder.setMdiArea(this)
    //     .addMdiSubWindow(new QLabel("MDI Sub Window", this), "Sub Window 1")
    //     .addMdiSubWindow(new QLabel("MDI Sub Window 2", this), "Sub Window 2");
    builder.useCentralWidget(this);
    builder.setGridLayout(this->centralWidget())
        .addTabWidget()
        .addTab(new QLabel("Central Widget", this), "Tab 1")
        .addTab(new QLabel("Central Widget", this), "Tab 2");
}

void MainWindow::setupDockWidgets()
{
    builder
        .addDockWidget(Qt::LeftDockWidgetArea, "Property", new QLabel("Left Dock Widget", this))
        .addDockWidget(
            Qt::RightDockWidgetArea, "Property", new QLabel("Right Dock Widget", this)
        );
}

void MainWindow::onShowOptionsDialog()
{
    MyDialog* dialog = new MyDialog(this);
    dialog->show();
}
